local function getupvaluetable(func, inname)
    local i = 1
    while true do
        local name, value = debug.getupvalue(func, i)
        if name == nil then
            return
        end
        
        if name == inname then
            return i,value
        end
        
        i=i+1
    end
end


local lua = _P.lua
local idx, old_can_add_friend = getupvaluetable(lua.handler.add_friend,'can_add_friend')
local _, MAX_FRIEND_LIMIT = getupvaluetable(old_can_add_friend,'MAX_FRIEND_LIMIT')
local _, uid_friends_map = getupvaluetable(old_can_add_friend,'uid_friends_map')

local function can_add_friend(uid,target_uid)
    local records = uid_friends_map[uid]
    if not records then
        return true
    end

    local friend_count = 0
    for _,_ in pairs(records) do
        friend_count = friend_count + 1
    end

    if friend_count >= MAX_FRIEND_LIMIT then
        return false,-1
    end

    return true
end

debug.setupvalue(lua.handler.add_friend,idx,can_add_friend)